﻿using System;
using System.Web.UI;
using Castle.ActiveRecord;
using Controllers;
using EntityLayer;
using EntityLayer.Entities;

/// <summary>
/// Summary description for BasePage
/// </summary>
public abstract class BasePage : Page, IView
{
    #region members
    protected bool IsUserLoggedIn
    {
        get { return LoggedInUser != null; }
    }
    protected User LoggedInUser
    {
        get { return Session["AppUser"] as User; }
        set { Session["AppUser"] = value; }
    }
    #endregion

    #region properties
    #endregion

    #region methods
    protected abstract Enumerations.UserPrivileges UserPrivileges { get; }
    public abstract void BindData();
    protected void Page_Load(object sender, EventArgs e)
    {
        EnforceSecurity();
        if (!IsPostBack)
            BindData();
    }
    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitializeSupervisingController();
        WireEvents();
    }
    /// <summary>
    /// Enforce users of the correct privilege are going to the correct place
    /// </summary>
    private void EnforceSecurity()
    {
        //everyone can access this page
        if(UserPrivileges == Enumerations.UserPrivileges.Null)
            return;
        //only logged in users can access this page
        if (UserPrivileges == Enumerations.UserPrivileges.StandardUser)
        {
            if(!IsUserLoggedIn)
                Response.Redirect("ProfilePage.aspx", true);
        }
    }
    protected abstract void WireEvents();
    protected abstract void InitializeSupervisingController();
    #endregion
}